var action=".htm";

jQuery(function(){
	//下载附件事件注册
	$(std.findTag('downloadFile')).click(function() {
		window.location.href = globePath+"/produce/downloadAttachment.htm?id="+$(this).attr('oid');
	});
});

jQuery(document).ready(function() {
	    jQuery("#file_template").uploadify({
	    'buttonText' 	: '上传样板',
		'buttonImg'		:  ctx+'/images/btn_upload_img.png',
        'swf'           : ctx+'/js/uploadify/uploadify.swf',
        'uploader'      : ctx+'/attachmentUpload/uploadBigAttachment.htm?fileType=2',
        'auto'			: true,
        'multi'    		: false,
        'removeCompleted' : false,
        'fileSizeLimit' : '1GB',
        'progressData' : 'speed',
        'uploadLimit' 	: 1,
        'queueSizeLimit' : 1,
        'fileTypeDesc'	: '*',
        'itemTemplate' 	: '<div id="${fileID}" class="c_fl c_ml5 wxb_tag c_bd_radius    c_omit">\<div class=" dk_sprite wxb_tag_close"  onclick="javascript:$(\'#${instanceID}\').uploadify(\'cancel\', \'${fileID}\')">\<a></a>\</div>\<span  id="file_div">${fileName} (${fileSize})</span><span class="data"></span>\</div>',
        'onUploadStart' : function(file) {
        	layer.msg("开始上传样板" + file.name,2,1);
        },
        'onUploadSuccess' : function(file, data, response) {
        	var json = jQuery.parseJSON(data);	 			
 			saveProduceAttachment(json['data'][0].id);
        },
        'onUploadError' : function(file, errorCode, errorMsg, errorString) {
        	layer.msg('The file ' + file.name + ' 无法上传: ' + errorString);
        }
        
  });
});

var saveProduceAttachment = function(attchmentId){
	var produceId = $("#produceId").val();
	var form = {};
	form.id = produceId;
	form.attachmentIds = attchmentId;
	ajaxCommFunWithDataType(globePath+"/produce/saveProduceAttachment"+action, form,'json',function(msg){
		if (msg.result == "success") {
			layer.msg("上传样板成功！",1,9,function(){
				window.location.href=globePath+"/produce/toAddProduceDetail."+action+"?produceId="+produceId;
			});
		} else {
			layer.alert(msg.result);
		};
	});
};

var toAddProduceDetail = function(){
	var fun =function(data){
		$("#detail-temp-div").html(data);
		
		//添加明细弹窗
		$.box('.onlyuserforaddtaskalertbox', {
			onOpen : function() {
				$.divselect("#divselect_ppp_plan", "#proPlanId");
				$.divselect("#divselect_ppp_user", "#userId");
				$("#proId").val($("#produceId").val());
				$('#scrollbar').tinyscrollbar();
				$('#scrollbar_002').tinyscrollbar();//里面有2个滚动条*/		
			}
		}, {
			submit:{
				close:false,
				dom:['#submitProduceDetail'],
				fun:function(index){
					var currObj = this;
					addProduceDetail(function(){					
						currObj.close(index);
					});
					
				}
			},
			close : {
				dom : [ '.onlyuserforaddtaskalertbox .closeforaddtask','.onlyuserforaddtaskalertbox .c_but_no' ]
			}
		});
	};
	
	var form={};
	form.produceId = $("#produceId").val();
	ajaxCommFunWithDataType(globePath+"/produce/loadProPlanAndUser"+action, form,'text',fun);
};


//添加明细
var addProduceDetail = function(callback){
	validate();//验证
	var fun = function(msg) {
		if (msg.result == "success") {
			if(callback){
				callback();
			}
			layer.msg("新建明细成功！",1,9,function(){
				window.location.href=globePath+"/produce/toAddProduceDetail."+action+"?produceId="+$("#produceId").val();
			});
		} else {
			layer.alert(msg.result);
		};
	};
	
	ajaxCommFunWithDataType(globePath+"/produce/submitPProduceDetail"+action, $("#addProduceDetailForm").serialize(),'json',fun);
};
//验证
var validate = function(){
	var proPlanId = $.trim($("#proPlanId").val());
	if(proPlanId==''){
		layer.msg("项目名称不能为空");
		return false;
	}
	var userId = $.trim($("#userId").val());
	if(userId==''){
		layer.msg("生产人员不能为空");
		return false;
	}
	var startTime = $.trim($("#startDate").val());
	if(startTime==''){
		layer.msg("起始时间不能为空");
		return false;
	}
	var endTime = $.trim($("#endDate").val());
	if(endTime==''){
		layer.msg("结束时间不能为空");
		return false;
	}
};

//删除明细
var deleteDetail = function(id){
	if(window.confirm("确定要删除该明细？")){
		var fun = function(msg) {
			if (msg.result == "success") {
				layer.msg("删除明细成功！",1,9,function(){
					window.location.href=globePath+"/produce/toAddProduceDetail."+action+"?produceId="+$("#produceId").val();
				});
			} else {
				layer.alert(msg.result);
			};
		};
		
		ajaxCommFunWithDataType(globePath+"/produce/deleteProduceDetail"+action, "produceDetailId="+id,'json',fun);
	}
};